package alon.xmnn.bobing;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.log4j.Logger;

import alon.app.ini.IniFile;
import alon.app.itask.IBrowserLisener;
import alon.app.itask.TERequest;
import alon.app.itask.Task;
public class Bobing2013 extends Task
{
	private static final long	serialVersionUID	= 2074398930361568345L;
	private Logger logInfo = Logger.getLogger("app.info");
	private Logger logError = Logger.getLogger("app.error");
	private String roomsLogin;
	private String boUrl="";
    private String curRooms="";
    private Random rand=new Random();
    private NnAccount curUser;
    private IBrowserLisener loginLisener;
    private IBrowserLisener boyibaLisener;
    private int[] coreIndexs={0,1,2,3,4,5,6,11};
    private String[] zhuanyuan={"带一","带三","带五","带六","带七","带八","带九","带十","带十一","带十二"};
    private String[] wuzi={"带一","带二","带三","带五","带六"};
    private List<Integer> willCore=new ArrayList<Integer>();
    private IBobingFinish reporter;
    public Bobing2013(NnAccount curUser,IniFile inifile, IBobingFinish reporter)
    {
    	this.curUser=curUser;
    	setName("bobing_"+curUser.getUser());
    	setIniFile(inifile);
    	this.reporter=reporter;
    }
    private void genCores(int tol,int cot){
		int x0=0,x1=0,x2=0,x3=0,x4=0,x5=0,x6=0;
		int x7=rand.nextInt(3)+1;
		if(tol/300<0){
			x7=0;
		}
		if(tol<=0){
			tol=900+rand.nextInt(50)*10;
			cot=10;
		}
		if(cot<=0){
			cot=10;
		}
		while((10*x1+20*x2+40*x3+60*x4+100*x5+200*x6+300*x7)<tol-100
				||(10*x1+20*x2+40*x3+60*x4+100*x5+200*x6+300*x7)>tol+100){
			x0=0;
			if((tol-x7*300)/200>0){
				x6=rand.nextInt((tol-x7*300)/200>(cot-x0-x7)?(cot-x0-x7):(tol-x7*300)/200);
			}
			if((tol-x7*300-x6*200)/100>0){
				x5=rand.nextInt((tol-x7*300-x6*200)/100>(cot-x0-x7-x6)?(cot-x0-x7-x6):(tol-x7*300-x6*200)/100);
			}
			if((tol-x7*300-x6*200-x5*100)/60>0){
				x4=rand.nextInt((tol-x7*300-x6*200-x5*100)/60>(cot-x0-x7-x6-x5)?(cot-x0-x7-x6-x5):(tol-x7*300-x6*200-x5*100)/60);
			}
			if((tol-x7*300-x6*200-x5*100-x4*60)/40>0){
				x3=rand.nextInt((tol-x7*300-x6*200-x5*100-x4*60)/40>(cot-x0-x7-x6-x5-x4)?(cot-x0-x7-x6-x5-x4):(tol-x7*300-x6*200-x5*100-x4*60)/40);
			}
			if((tol-x7*300-x6*200-x5*100-x4*60-x3*40)/20>0){
				x2=rand.nextInt((tol-x7*300-x6*200-x5*100-x4*60-x3*40)/20>(cot-x0-x7-x6-x5-x4-x3)?(cot-x0-x7-x6-x5-x4-x3):(tol-x7*300-x6*200-x5*100-x4*60-x3*40)/20);
			}
			x1=(cot-x0-x7-x6-x5-x4-x3-x2)/2+rand.nextInt(cot-x0-x7-x6-x5-x4-x3-x2/2);
			x0=cot-x7-x6-x5-x4-x3-x2-x1;
		}
		logInfo.info(curUser.getUser()+",预计博:"+(x0+x1+x2+x3+x4+x5+x6+x7)+"次,预计成绩:"+(10*x1+20*x2+40*x3+60*x4+100*x5+200*x6+300*x7));
		for(int i=0;i<x0;i++){
			if(willCore.size()!=0){
				willCore.add(rand.nextInt(willCore.size()),coreIndexs[0]);
			}else{
				willCore.add(coreIndexs[0]);
			}
		}
		for(int i=0;i<x1;i++){
			if(willCore.size()!=0){
				willCore.add(rand.nextInt(willCore.size()),coreIndexs[1]);
			}else{
				willCore.add(coreIndexs[1]);
			}
		}
		for(int i=0;i<x2;i++){
			if(willCore.size()!=0){
				willCore.add(rand.nextInt(willCore.size()),coreIndexs[2]);
			}else{
				willCore.add(coreIndexs[2]);
			}
		}
		for(int i=0;i<x3;i++){
			if(willCore.size()!=0){
				willCore.add(rand.nextInt(willCore.size()),coreIndexs[3]);
			}else{
				willCore.add(coreIndexs[3]);
			}
		}
		for(int i=0;i<x4;i++){
			if(willCore.size()!=0){
				willCore.add(rand.nextInt(willCore.size()),coreIndexs[4]);
			}else{
				willCore.add(coreIndexs[4]);
			}
		}
		for(int i=0;i<x5;i++){
			if(willCore.size()!=0){
				willCore.add(rand.nextInt(willCore.size()),coreIndexs[5]);
			}else{
				willCore.add(coreIndexs[5]);
			}
		}
		for(int i=0;i<x6;i++){
			if(willCore.size()!=0){
				willCore.add(rand.nextInt(willCore.size()),coreIndexs[6]);
			}else{
				willCore.add(coreIndexs[6]);
			}
		}
		for(int i=0;x7>0 && i<x7;i++){
			if(willCore.size()!=0){
				willCore.add(rand.nextInt(willCore.size()),coreIndexs[7]);
			}else{
				willCore.add(coreIndexs[7]);
			}
		}
    }
	public void init() throws Exception
	{
		boUrl=getConfValue("bobing", "boUrl", "http://bobing.xmnn.cn/api/bbsave.php");
		roomsLogin=getConfValue("bobing", "roomsLogin222", "http://bobing.xmnn.cn/api/xmnn.php");
		initLiseners();
	}
	public void login(){
		try {
			Map<String, String> param=new HashMap<String, String>();
			param.put("type","厦门网会员");
			param.put("username",curUser.getUser());
			param.put("password", curUser.getPass());
			TERequest rq=new TERequest( Xmnn.COOKIE_PREFIX+curUser.getUser(),
					roomsLogin, TERequest.METHOD_POST,"utf-8", "gb2312");
			rq.setMethod(TERequest.METHOD_POST);
			rq.addParam(param);
			doConnect(loginLisener,rq,iapp.getProxy());
		} catch (Exception e) {
			logError.error("切换包间失败",e);
		}
	}
	private int curindex=0;
	public void boyiba(){
		try {
			if(willCore.size()>0){
				curindex=willCore.remove(0);
			}
			String str="很遗憾，没有获奖，继续努力！";
			String rs="";
			String point="";
			if(curindex==6){
				curindex=6+rand.nextInt(5);
			}else if(curindex==11){
				curindex=11+rand.nextInt(1);
			}
			Core core=Core.getCore(curindex);
			if(curindex>0){
				str=curUser.getUser()+",恭喜您！您博得了";
				rs=core.getStr();
				point=core.getCore();
				if(curindex==6){
					rs+=zhuanyuan[rand.nextInt(zhuanyuan.length)];
				}else if(curindex==7){
					rs+=wuzi[rand.nextInt(wuzi.length)];
				}
			}
			TERequest rq=new TERequest( Xmnn.COOKIE_PREFIX+curUser.getUser(),
					boUrl, TERequest.METHOD_POST,"gb2312", "gb2312");
			rq.addParam("username",curUser.getUser());
			rq.addParam("point",point);
			rq.addParam("rs",rs);
			rq.addParam("str", str);
			doConnect(boyibaLisener,rq,iapp.getProxy());
			logInfo.info(curUser.getUser()+",房间:"+curRooms+","+str+ rs+(point.equals("")?"":",博饼分:"+point)+",还有包间:"+curUser.getRooms().size()+"还能博:"+willCore.size());
		} catch (Exception e) {
			logError.error("博一把出错",e);
		}
	}
	public void taskError(String error, Exception e) {
		logError.error("博饼异常",e);
	}
	public void taskCore() throws Exception {
		login();
	}
	public void taskDestroy() throws Exception {
	}
	private void initLiseners(){
		if(loginLisener==null){
			loginLisener=new IBrowserLisener(){
				public void doException(String msg,Exception e){
					logError.error(msg,e);
				}
				public void response(String resp)
				{
					if("0".equals(resp)){
						logInfo.info("博饼区登录出错");
					}else{
						logInfo.info("博饼区已成功登录");
						genCores(curUser.getWillCore(),curUser.getRooms().size()*10);
						gotoSleep(3+rand.nextInt(3));
						boyiba();
					}
				}
			};
		}
		if(boyibaLisener==null){
			boyibaLisener=new IBrowserLisener(){
				public void doException(String msg,Exception e){
					logError.error(msg,e);
					gotoSleep(3+rand.nextInt(3));
					willCore.add(curindex);
					boyiba();
				}
				public void response(String resp)
				{
					String[] tmp=resp.split(",");
					if(tmp.length>1&&tmp[1].matches("[-,0-9]*")
							&&Integer.parseInt(tmp[1])>0 && Integer.parseInt(tmp[1])<10){
						System.out.println("博饼分提交成功,已博次数:"+tmp[1]);
						gotoSleep(3+rand.nextInt(3));
						boyiba();
					}else{
						System.out.println("返回:"+resp);
						reporter.finish(curUser);
					}
				}
			};
		}
	}
}
